package com.zhaowb.springcloud.admin.rpc;

import com.alibaba.fastjson.JSONObject;
import com.zhaowb.springcloud.admin.biz.ElementBiz;
import com.zhaowb.springcloud.admin.biz.MenuBiz;
import com.zhaowb.springcloud.admin.biz.UserBiz;
import com.zhaowb.springcloud.admin.constant.CommonConstant;
import com.zhaowb.springcloud.admin.entity.Element;
import com.zhaowb.springcloud.admin.entity.Menu;
import com.zhaowb.springcloud.admin.entity.User;
import com.zhaowb.springcloud.admin.vo.MenuTree;
import com.zhaowb.springcloud.api.vo.authority.PermissionInfo;
import com.zhaowb.springcloud.api.vo.user.UserInfo;
import com.zhaowb.springcloud.common.util.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IDEA
 *
 * @author zhaowb
 * @date 2018/11/7 21:35
 */
@Controller
@RequestMapping("api")
public class UserService {

    @Autowired
    private UserBiz userBiz;

    @Autowired
    private MenuBiz menuBiz;

    @Autowired
    private ElementBiz elementBiz;

    @ResponseBody
    @RequestMapping(value = "/user/username/{username}", method = RequestMethod.GET, produces = "application/json")
    public UserInfo getUserByUsername(@PathVariable("username") String username) {
        UserInfo userInfo = new UserInfo();
        User user = userBiz.getUserByUsername(username);
        BeanUtils.copyProperties(user, userInfo);
        userInfo.setId(user.getId().toString());
        return userInfo;
    }

    @ResponseBody
    @RequestMapping(value = "permissions", method = RequestMethod.GET)
    public List<PermissionInfo> getAllPermission() {
        List<Menu> menus = menuBiz.selectListAll();
        List<PermissionInfo> result = new ArrayList<>();
        menu2permission(menus, result);
        List<Element> elements = elementBiz.selectListAll();
        element2permission(result, elements);
        return result;
    }

    public void menu2permission(List<Menu> menus, List<PermissionInfo> result) {
        PermissionInfo info;
        for (Menu menu : menus) {
            if (StringUtils.isBlank(menu.getHref())) {
                continue;
            }
            info = new PermissionInfo();
            info.setCode(menu.getCode());
            info.setType(CommonConstant.RESOURCE_TYPE_MENU);
            info.setName(CommonConstant.RESOURCE_ACTION_VISIT);
            String uri = menu.getHref();
            if (!uri.startsWith("/")) {
                uri = "/" + uri;
            }
            info.setUri(uri);
            info.setMethod(CommonConstant.RESOURCE_REQUEST_METHOD_GET);
            info.setMenu(menu.getTitle());
            result.add(info);
        }
    }

    private void element2permission(List<PermissionInfo> result, List<Element> elements) {
        PermissionInfo info;
        for (Element element : elements) {
            info = new PermissionInfo();
            info.setCode(element.getCode());
            info.setType(element.getType());
            info.setUri(element.getUri());
            info.setMethod(element.getMethod());
            info.setName(element.getName());
            info.setMenu(element.getMenuId());
            result.add(info);
        }
    }

    @ResponseBody
    @RequestMapping(value = "/user/un/{username}/permissions", method = RequestMethod.GET)
    public List<PermissionInfo> getPermissionByUsername(@PathVariable("username") String username) {
        User user = userBiz.getUserByUsername(username);
        List<Menu> menus = menuBiz.getUserAuthorityMenuByUserId(user.getId());
        List<PermissionInfo> result = new ArrayList<>();
        menu2permission(menus, result);
        List<Element> elements = elementBiz.getAuthorityElementByUserId(user.getId() + "");
        element2permission(result, elements);
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/user/un/{username}/system", method = RequestMethod.GET)
    public String getSystemsByUsername(@PathVariable("username") String username) {
        int userId = userBiz.getUserByUsername(username).getId();
        return JSONObject.toJSONString(menuBiz.getUserAuthoritySystemByUserId(userId));
    }

    @ResponseBody
    @RequestMapping(value = "/user/un/{username}/menu/parent/{parentId}", method = RequestMethod.GET)
    public String getMenusByUsername(@PathVariable("username") String username, @PathVariable("parentId") Integer parentId) {
        int userId = userBiz.getUserByUsername(username).getId();
        try {
            if (parentId == null || parentId < 0) {
                parentId = menuBiz.getUserAuthoritySystemByUserId(userId).get(0).getId();
            }
        } catch (Exception e) {
            return JSONObject.toJSONString(new ArrayList<MenuTree>());
        }
        return JSONObject.toJSONString(getMenuTree(menuBiz.getUserAuthorityMenuByUserId(userId), parentId));
    }

    private List<MenuTree> getMenuTree(List<Menu> menus, int root) {
        List<MenuTree> trees = new ArrayList<MenuTree>();
        MenuTree node = null;
        for (Menu menu : menus) {
            node = new MenuTree();
            BeanUtils.copyProperties(menu, node);
            trees.add(node);
        }
        return TreeUtil.build(trees, root);
    }
}
